home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2005 October / PCWOCT05.iso / Software / FromTheMag / XAMPP 1.4.14 / xampp-win32-1.4.14-installer.exe / xampp / php / pear / Log / sqlite.php < prev    next >
PHP Script  |  2004-10-01  |  8KB  |  239 lines

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. // +----------------------------------------------------------------------+
  4. // | PHP version 4.0                                                      |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2004 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.0 of the PHP license,       |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available at through the world-wide-web at                           |
  11. // | http://www.php.net/license/2_02.txt.                                 |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Bertrand Mansion <bmansion@mamasam.com>                     |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: sqlite.php,v 1.3 2004/01/19 08:02:40 jon Exp $
  20.  
  21. /**
  22.  * The Log_sqlite class is a concrete implementation of the Log::
  23.  * abstract class which sends messages to an Sqlite database.
  24.  * Each entry occupies a separate row in the database.
  25.  *
  26.  * This implementation uses PHP native Sqlite functions.
  27.  *
  28.  * CREATE TABLE log_table (
  29.  *  id          INTEGER PRIMARY KEY NOT NULL,
  30.  *  logtime     NOT NULL,
  31.  *  ident       CHAR(16) NOT NULL,
  32.  *  priority    INT NOT NULL,
  33.  *  message
  34.  * );
  35.  *
  36.  * @author  Bertrand Mansion <bmansion@mamasam.com>
  37.  * @author  Jon Parise <jon@php.net>
  38.  * @since   Log 1.8.3
  39.  * @package Log
  40.  *
  41.  * @example sqlite.php      Using the Sqlite handler.
  42.  */
  43. class Log_sqlite extends Log
  44. {
  45.     /**
  46.      * Array containing the connection defaults
  47.      * @var array
  48.      * @access private
  49.      */
  50.     var $_options = array('mode'       => 0666,
  51.                           'persistent' => false);
  52.  
  53.     /**
  54.      * Object holding the database handle.
  55.      * @var object
  56.      * @access private
  57.      */
  58.     var $_db = null;
  59.  
  60.     /**
  61.      * Flag indicating that we're using an existing database connection.
  62.      * @var boolean
  63.      * @access private
  64.      */
  65.     var $_existingConnection = false;
  66.  
  67.     /**
  68.      * String holding the database table to use.
  69.      * @var string
  70.      * @access private
  71.      */
  72.     var $_table = 'log_table';
  73.  
  74.  
  75.     /**
  76.      * Constructs a new sql logging object.
  77.      *
  78.      * @param string $name         The target SQL table.
  79.      * @param string $ident        The identification field.
  80.      * @param mixed  $conf         Can be an array of configuration options used
  81.      *                             to open a new database connection
  82.      *                             or an already opened sqlite connection.
  83.      * @param int    $level        Log messages up to and including this level.
  84.      * @access public     
  85.      */
  86.     function Log_sqlite($name, $ident = '', &$conf, $level = PEAR_LOG_DEBUG)
  87.     {
  88.         $this->_id = md5(microtime());
  89.         $this->_table = $name;
  90.         $this->_ident = $ident;
  91.         $this->_mask = Log::UPTO($level);
  92.  
  93.         if (is_array($conf)) {
  94.             foreach ($conf as $k => $opt) {
  95.                 $this->_options[$k] = $opt;
  96.             }
  97.         } else {
  98.             // If an existing database connection was provided, use it.
  99.             $this->_db =& $conf;
  100.             $this->_existingConnection = true;
  101.         }
  102.     }
  103.  
  104.     /**
  105.      * Opens a connection to the database, if it has not already
  106.      * been opened. This is implicitly called by log(), if necessary.
  107.      *
  108.      * @return boolean   True on success, false on failure.
  109.      * @access public     
  110.      */
  111.     function open()
  112.     {
  113.         if (is_resource($this->_db)) {
  114.             $this->_opened = true;
  115.             return $this->_createTable();
  116.         } else {
  117.             /* Set the connection function based on the 'persistent' option. */
  118.             if (empty($this->_options['persistent'])) {
  119.                 $connectFunction = 'sqlite_open';
  120.             } else {
  121.                 $connectFunction = 'sqlite_popen';
  122.             }
  123.  
  124.             /* Attempt to connect to the database. */
  125.             if ($this->_db = $connectFunction($this->_options['filename'],
  126.                                               (int)$this->_options['mode'],
  127.                                               $error)) {
  128.                 $this->_opened = true;
  129.                 return $this->_createTable();
  130.             }
  131.         }
  132.  
  133.         return $this->_opened;
  134.     }
  135.  
  136.     /**
  137.      * Closes the connection to the database if it is still open and we were
  138.      * the ones that opened it.  It is the caller's responsible to close an
  139.      * existing connection that was passed to us via $conf['db'].
  140.      *
  141.      * @return boolean   True on success, false on failure.
  142.      * @access public     
  143.      */
  144.     function close()
  145.     {
  146.         /* We never close existing connections. */
  147.         if ($this->_existingConnection) {
  148.             return false;
  149.         }
  150.  
  151.         if ($this->_opened) {
  152.             $this->_opened = false;
  153.             sqlite_close($this->_db);
  154.         }
  155.  
  156.         return ($this->_opened === false);
  157.     }
  158.  
  159.     /**
  160.      * Inserts $message to the currently open database.  Calls open(),
  161.      * if necessary.  Also passes the message along to any Log_observer
  162.      * instances that are observing this Log.
  163.      *
  164.      * @param mixed  $message  String or object containing the message to log.
  165.      * @param string $priority The priority of the message.  Valid
  166.      *                  values are: PEAR_LOG_EMERG, PEAR_LOG_ALERT,
  167.      *                  PEAR_LOG_CRIT, PEAR_LOG_ERR, PEAR_LOG_WARNING,
  168.      *                  PEAR_LOG_NOTICE, PEAR_LOG_INFO, and PEAR_LOG_DEBUG.
  169.      * @return boolean  True on success or false on failure.
  170.      * @access public     
  171.      */
  172.     function log($message, $priority = null)
  173.     {
  174.         /* If a priority hasn't been specified, use the default value. */
  175.         if ($priority === null) {
  176.             $priority = $this->_priority;
  177.         }
  178.  
  179.         /* Abort early if the priority is above the maximum logging level. */
  180.         if (!$this->_isMasked($priority)) {
  181.             return false;
  182.         }
  183.  
  184.         /* If the connection isn't open and can't be opened, return failure. */
  185.         if (!$this->_opened && !$this->open()) {
  186.             return false;
  187.         }
  188.  
  189.         // Extract the string representation of the message.
  190.         $message = $this->_extractMessage($message);
  191.  
  192.         // Build the SQL query for this log entry insertion.
  193.         $q = sprintf('INSERT INTO [%s] (logtime, ident, priority, message) ' .
  194.                      "VALUES ('%s', '%s', %d, '%s')",
  195.                      $this->_table,
  196.                      strftime('%Y-%m-%d %H:%M:%S', time()),
  197.                      sqlite_escape_string($this->_ident),
  198.                      $priority,
  199.                      sqlite_escape_string($message));
  200.         if (!($res = @sqlite_unbuffered_query($this->_db, $q))) {
  201.             return false;
  202.         }
  203.         $this->_announce(array('priority' => $priority, 'message' => $message));
  204.  
  205.         return true;
  206.     }
  207.  
  208.     /**
  209.      * Checks whether the log table exists and creates it if necessary.
  210.      *
  211.      * @return boolean  True on success or false on failure.
  212.      * @access private
  213.      */
  214.     function _createTable()
  215.     {
  216.         $q = "SELECT name FROM sqlite_master WHERE name='" . $this->_table .
  217.              "' AND type='table'";
  218.  
  219.         $res = sqlite_query($this->_db, $q);
  220.  
  221.         if (sqlite_num_rows($res) == 0) {
  222.             $q = 'CREATE TABLE [' . $this->_table . '] (' .
  223.                  'id INTEGER PRIMARY KEY NOT NULL, ' .
  224.                  'logtime NOT NULL, ' .
  225.                  'ident CHAR(16) NOT NULL, ' .
  226.                  'priority INT NOT NULL, ' .
  227.                  'message)';
  228.  
  229.             if (!($res = sqlite_unbuffered_query($this->_db, $q))) {
  230.                 return false;
  231.             }
  232.         }
  233.  
  234.         return true;
  235.     }
  236. }
  237.  
  238. ?>
  239.